import {Component, EventEmitter, Input, Output} from '@angular/core';
import {CartProvider} from "../../providers/cartProvider";
import {AlertController, NavController} from "ionic-angular";
import {AlertPage} from "../../employee/pages/alert/alert";
import {CustomerProvider} from "../../providers/customerProvider";
import {EmployeeProvider} from "../../providers/employeeProvider";
import {DishProvider} from "../../providers/DishProvider";
import {Dish} from "../../models/Dish";


/**
 * Generated class for the SpinnerComponent component.
 *
 * See https://angular.io/docs/ts/latest/api/core/index/ComponentMetadata-class.html
 * for more info on Angular Components.
 */
@Component({
  selector: 'spinner',
  templateUrl: 'spinner.html'
})
export class SpinnerComponent {

  @Output() onNumChanged = new EventEmitter<number>();
  @Input() num = 0;
  Table_ID = "???";
  @Input() max = 0;
  Min = "min";
  Max = "up";
  dish: Dish[];

  constructor(public cartPro: CartProvider,
              public alerCtrl: AlertController,
              public navCtrl: NavController,
              public customer: CustomerProvider,
              public dishProd: DishProvider,
              public employee: EmployeeProvider) {
    this.Table_ID = this.cartPro.Table_ID();
  }


  down() {
    if (this.employee.check() ) {
      if (this.Table_ID == null) {
        let alert = this.alerCtrl.create({
            message: 'Please choose the Table_ID',
            buttons: [{
              text: 'go to-settings',
              handler: () => {
                this.navCtrl.push(AlertPage);
              }
            }]
          })
        ;
        alert.present();
        return;
      }
      else {
        if (this.num > 0) {
          this.num = this.num - 1;
          this.max = this.max + 1;

          /*加号变化*/
          if (this.max > 0) {
            this.Max = "up"
          }
          if (this.max < 1) {
            this.Max = "max"
          }


          /*减号变化*/
          if (this.num < 1) {
            this.Min = "min"
          }
          if (this.num > 1) {
            this.Min = "down"
          }
          this.onNumChanged.emit(this.num);
        }
      }
    }
    if(this.customer.check()) {
      if (this.num > 0) {


        this.num = this.num - 1;
        this.max = this.max + 1;
        console.log('this.num',this.num);

        /*加号变化*/
        if (this.max > 0) {
          this.Max = "up"
        }
        if (this.max < 1) {
          this.Max = "max"
        }

        /*减号变化*/
        if (this.num < 1) {
          this.Min = "min"
        }
        if (this.num > 1) {
          this.Min = "down";
        }
        this.onNumChanged.emit(this.num);
      }
    }
  }

  up() {
    if (this.employee.check() ) {
      if (this.Table_ID == null) {
        let alert = this.alerCtrl.create({
            message: 'Please choose the Table_ID',
            buttons: [{
              text: 'go to-settings',
              handler: () => {
                this.navCtrl.push(AlertPage);
              }
            }]
          })
        ;
        alert.present();
        return;
      }
      else {
        if (this.max > 0) {
          if (this.num < 9999) {
            this.num = this.num + 1;
            this.onNumChanged.emit(this.num);
            this.max = this.max - 1;

            /*加号变化*/
            if (this.max > 0) {
              this.Max = "up"
            }
            if (this.max < 1) {
              this.Max = "max"
            }

            /*减号变化*/
            if (this.num < 1) {
              this.Min = "min"
            }
            if (this.num > 0) {
              this.Min = "down";
            }





          }
        }
        if (this.max < 1) {
          let alert = this.alerCtrl.create({
            message: '0ops! Sorry.....This dish is temporarily unavailable',

          });
          alert.present();
        }

      }
    }

    if( this.customer.check()) {
      if (this.max > 0) {
        if (this.num < 99) {
          this.num = this.num + 1;
          this.max = this.max - 1;
          console.log('this.num',this.num);

          /*加号变化*/
          if (this.max < 1) {
            this.Max = "max"
          }

          /*减号变化*/
          if (this.num < 1) {
            this.Min = "min"
          }
          if (this.num > 0) {
            this.Min = "down";
          }

          this.onNumChanged.emit(this.num);
        }
      }
      if (this.max < 1) {
        let alert = this.alerCtrl.create({
          message: '0ops! Sorry.....This dish is temporarily unavailable',

        });
        alert.present();
      }
    }
  }

  keyUp() {
    if (this.num < 0)
      this.num = 0;
    if (this.num > this.max)
      this.num = this.max;
    this.max=this.max-this.num;
    this.onNumChanged.emit(this.num);
  }

  wipe() {
    if (this.employee.check() ) {
      if (this.Table_ID == null) {
        let alert = this.alerCtrl.create({
            message: 'Please choose the Table_ID',
            buttons: [{
              text: 'go to-settings',
              handler: () => {
                this.navCtrl.push(AlertPage);
              }
            }]
          })
        ;
        alert.present();
        return;
      }
      else
        this.num = null;
    }
    if(this.customer.check()){
      this.num = null;

    }

  }
}
